/**
 * Created by wuye on 2017/12/11.
 */


var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        },

    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            bookname: null
        },
        showList: true,
        title:null,
        // roleList:{},
        banner:{
            bannerpic:null
        },
        sels:[

        ],
        showInfo:true,
        userinfo:{
            profileimageurl:''
        },
        bankCardBtn:'去绑定',
        codeWord:'获取验证码',
        bankCard:null,
        name:'',
        bankcardnum:'',
        mobile:'',
        code:'',
        idNum:'',
        count:0,
    },
    created:function () {
        $.ajax({
            url: baseURL + 'sys/user/userInfo',
            type: "POST",
            contentType: "application/json",
            success: function(result) {
                if (result.code===0){
                    vm.userinfo=result.user
                    console.log(result)
                    vm.getBankCard()
                } else {
                    vm.userinfo={}
                    alert(result.msg)
                }
            },
            error: function(error) {
                alert('数据加载失败!');
            }
        })
    },
    methods: {
        bindCard:function(){
            this.title='绑定银行卡'
            this.showInfo=false;
        },
        getBankCard:function(){
            $.ajax({
                url: baseURL + 'sys/bankcard/getBankCard',
                type: "POST",
                contentType: "application/json",
                success: function(result) {
                    console.log(result)
                    if(result.code==0){
                        if(result.bankcard){
                            vm.bankCardBtn='更换'
                            vm.bankCard=result.bankcard.bankcard.substring(0,3)+'****'+result.bankcard.bankcard.substring(result.bankcard.bankcard.length-4,result.bankcard.bankcard.length)+'('+result.bankcard.bankname+")"
                        }
                    }else{
                        alert(result.msg)
                    }
                },
                error: function(error) {
                    alert('数据加载失败!');
                }
            })
        },
        getCode:function(){
            if (vm.mobile) {
                var THIS = this;
                var obj = {
                    phone: THIS.mobile
                };
                $.ajax({
                    url:  '/youxiang/api/getCode?'+'phone='+obj.phone,
                    type: "GET",
                    contentType: "application/json",
                    success: function(result) {
                        if(result.code==0){
                            vm.counting()
                            alert('发送成功，请注意查收短信')
                        }else {
                            alert('发送失败，请稍后重试')
                        }
                    },
                    error: function(error) {
                        alert('数据加载失败!');
                    }
                });
            } else {
                alert("请输入正确格式手机号");
            }
        },
        // 倒计时
        counting:function() {
            setTimeout(function(){
                vm.count=60;
                var counting=setInterval(function () {
                    vm.count=vm.count-1;
                    vm.codeWord=vm.count+"s秒后重发";
                    if(!vm.count){
                        clearInterval(counting);
                        vm.codeWord='获取验证码';
                        vm.count=0;
                    }
                },1000)
            },500)
        },
        changeImage: function(e) {
            var file = e.target.files[0]
            var reader = new FileReader()
            var THIS = this
            reader.readAsDataURL(file)
            reader.onload = function(e) {
                // this.result 为图片
                THIS.upload(this.result)
            }
        },
        upload: function(data) {
            // 上传图片
            var THIS = this;
            if(this.$refs.file.files.length !== 0) {
                var image = new FormData()
                image.append('file', this.$refs.file.files[0])

                $.ajax({
                    url: '/youxiang/api/upload/up',
                    data: image,
                    // headers: {
                    //     "Content-Type": "multipart/form-data"
                    // },
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'post',
                    success: function(result) {
                        THIS.banner.bannerpic = result.url
                    },
                    error: function(error) {
                        alert('图片上传失败!');
                    }
                })
            }
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.banner.bannerpic=null;
            vm.banner.bookname=null;
            vm.banner.id=null;
        },
        query: function () {
            vm.reload();
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.getBanner(id);

        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/banner/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        update2: function () {
            var url = "sys/banner/update2";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.category),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getBanner: function(id){
            $.get(baseURL + "sys/banner/findById/"+id, function(r){
                vm.banner = r.banner;
            });
        },
        saveOrUpdate: function () {
            var sfile = new FormData()
            sfile.append('name', vm.name)
            sfile.append('bankcard', vm.bankcardnum);
            // sfile.append('isPay', _this.inputs[one]['checkbox']);
            sfile.append('phone', vm.mobile);
            sfile.append('identity', vm.idNum);
            sfile.append('code', vm.code)
            $.ajax({
                url: baseURL + '/sys/bankcard/addBankCard',
                data: sfile,
                // headers: {
                //"Content-Type": "multipart/form-$refs"
                // "Content-Type": "text/plain;charset=UTF-8"
                // },
                cache: false,
                processData: false,
                contentType: false,
                type: 'post',
                success: function(r){
                    if(r.code==0){
                        vm.getBankCard()
                        alert('操作成功')
                        vm.showInfo = true;
                    }else {
                        alert(r.msg)
                    }
                }
            });
        },
        reload: function () {
            vm.showInfo = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'bookname': vm.q.bookname},
                page:page
            }).trigger("reloadGrid");
        }
    }
});
